package com.hotleproject.controller;

import com.hotleproject.pojo.Comment;
import com.hotleproject.pojo.Users;
import com.hotleproject.service.CommentService;
import com.hotleproject.service.UsersService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

//创建controller
@Controller
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UsersService usersService;

    //根据用户id查询用户5,修改controller
    @RequestMapping("/{page}")
    public String showPage(@PathVariable String page) {
        return page;
    }

    @RequestMapping(value = "login.action")
    public void login(Users users, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
//        ModelAndView mav = new ModelAndView();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        users.setUsername(username);
        users.setPassword(password);
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = response.getWriter();
        users = this.usersService.findUserById(username, password);
        if (users != null) {
            session.setAttribute("users", users.getUsername());
            out.print("<script language=\"javascript\">alert('登录成功，欢迎您！');window.location.href='/users/sessionDeal'</script>");

//            mav.setViewName("manage");
//            return mav;
        } else {
            out.print("<script language=\"javascript\">alert('用户名或密码错误，请重新登录！');window.location.href='/users/UserLogin'</script>");
//            session.setAttribute("errormsg", "账号或密码错误!请重新输入");
        }
//        mav.setViewName("UserLogin");
//        return mav;

    }

    @RequestMapping("/sessionDeal")
    public String sessionDeal(Model model, HttpSession session) {
        String users = session.getAttribute("users").toString();
        model.addAttribute("users", users);
        return "HotleMain";
    }

    @Autowired
    private CommentService commentService;

    @RequestMapping("/UserCount")
    public String UserCount(HttpServletRequest request,Model model, HttpSession session) {
        if(session.getAttribute("users")==null)
            return "please";
        String username = session.getAttribute("users").toString();
        model.addAttribute("username", username);
        String phone = this.usersService.findPhoneByUser(username);
        model.addAttribute("phone", phone);

//用户评论
        String spPage = request.getParameter("pageNumber");
        //设置每页条数
        int pageSize = 10;
        //页数
        int pageNo = 0;
        if (spPage == null) {
            pageNo = 1;
        } else {
            pageNo = Integer.valueOf(spPage);
            if (pageNo < 1) {
                pageNo = 1;
            }
        }
        //设置最大页数
        int totalCount = 0;
        int count =  this.commentService.getUserCommentCount();
        if (count > 0) {
            totalCount = count;
        }
        int maxPage = totalCount % pageSize == 0 ? 1 : totalCount / pageSize + 1;
        if (pageNo > maxPage) {
            pageNo = maxPage;
        }
        int tempPageNo = (pageNo - 1) * pageSize;


////        String hotlename = session.getAttribute("adminis").toString();
//        model.addAttribute("hotlename", hotlename);
//////        model.addAttribute("pageNo1", pageNo);
//        model.addAttribute("pageNo", tempPageNo);
//        model.addAttribute("pageSize", pageSize);
//        return "text";
        List<Comment> list=this.commentService.selectUserComment(username,tempPageNo,pageSize);
//
//        //最后把信息放入model转发到页面把信息带过去
//        model.addAttribute("list",list);
        model.addAttribute("list",list);
        model.addAttribute("pageNo",pageNo);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("maxPage",maxPage);
        return "HotleUser";
    }

    @RequestMapping("/addUser")
    public void addUsers(HttpServletResponse response, Users users, HttpServletRequest request, Model model) throws IOException {
        this.usersService.addUser(users);
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = response.getWriter();
        out.print("<script language=\"javascript\">alert('注册成功，返回登录界面！');window.location.href='/users/UserLogin'</script>");


//        model.addAttribute("username",request.getParameter("username"));
//        model.addAttribute("password",request.getParameter("password"));
//        model.addAttribute("phone", request.getParameter("phone"));

//        return "text";
    }
}
